package com.qf.controller;

import com.qf.dto.R;
import com.qf.pojo.Category;
import com.qf.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/*
杨威
2021/12/31
21:42
*/
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    CategoryService categoryService;
    @RequestMapping("/add")
    public R addCategory(@RequestBody Category category){
        categoryService.addCategory(category);

        return R.setOk();
    }

    @RequestMapping("/selectAll")
    public R getAllDataCategory(){
        List<Category> categoryList = categoryService.selectAllCategories();
        return R.setOk(categoryList);
    }

    @RequestMapping("/selectById")
    public R getCategoryById(Long id){
        Category category = categoryService.selectById(id);
        return R.setOk(category);
    }

    @RequestMapping("/selectByNameLike")
    public R getCategoryByNameLike(String cName){
        List<Category> categoryList = categoryService.selectCategoriesByNameLike(cName);
        return R.setOk(categoryList);
    }

    @RequestMapping("/deleteByIds")
    public R deleteCategoryByIds(@RequestBody List<Long> ids){
        int i = categoryService.deleteCategoriesByIds(ids);
        return R.setOk();
    }

    @RequestMapping("/updateCategory")
    public R updateCategory(@RequestBody Category category){
        System.out.println("category = " + category);
        int i = categoryService.updateCategory(category);
        return R.setOk(i);
    }
}
